package com.michaelbulava.TFS.ui.helpers;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.util.PlatformIcons;
import com.michaelbulava.TFS.vc.tfsitem.TFSFile;
import com.michaelbulava.TFS.vc.tfsitem.TFSFolder;
import com.michaelbulava.TFS.vc.tfsitem.TFSItem;
import com.microsoft.tfs.core.clients.versioncontrol.path.ServerPath;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.ExtendedItem;

import javax.swing.*;
import java.io.File;

/**
 * Created by MichaelA on 7/22/2015.
 */
public class IconHelper {
    private static final String JAVA = "java";
    private static final String XML = "xml";
    private static final String JAR = "jar";
    private static final String HTML = "html";
    private static final String HTM = "htm";
    private static final String XSD = "xsd";
    private static final String JSON = "json";
    private static final String JS = "js";
    private static final String JSP = "jsp";
    private static final String JSPX = "jspx";
    private static final String TXT = "txt";
    private static final String LOG = "log";
    private static final String FORM = "form";
    private static final String DTD ="dtd";
    private static final String CSS = "css";
    private static final String MF = "mf";
    private static final String WSDL = "wsdl";
    private static final String CONFIG = "config";
    private static final String PROPERTIES = "properties";



    public static Icon getIconForTFSItem(TFSItem item){
        if (item == null)
            return null;
        String serverPath = ServerPath.canonicalize(item.getFullPath());
        ExtendedItem extendedItem = item.getExtendedItem();

        if (item instanceof TFSFolder){
            if (extendedItem != null && extendedItem.isBranch()){
                if (item.isDeleted()){
                    return IconLoader.getIcon("/images/vc/folder_branch_deleted.gif");
                }
                return IconLoader.getIcon("/images/vc/folder_branch.gif");
            }
            if (ServerPath.equals(ServerPath.ROOT, item.getFullPath()))
                return IconLoader.getIcon("/images/common/team_foundation_server.gif");
            if (ServerPath.equals(serverPath, ServerPath.getTeamProject(serverPath)))
                return IconLoader.getIcon("/images/common/team_project.gif");
            if (item.isDeleted())
                return IconLoader.getIcon("/images/vc/folder_deleted.gif");
            return PlatformIcons.FOLDER_ICON;
        }
        if (item instanceof TFSFile){
            return getIconForFile(ServerPath.getFileName(serverPath));
        }
        return null;
    }

    public static Icon getIconForFile(String fileName){
        File file = new File(fileName);
        String extension = FileUtilRt.getExtension(file.getName());
        return getIconForFileExtension(extension);
    }

    public static Icon getIconForFile(File file){
        String extension  = FileUtilRt.getExtension(file.getName());
        return getIconForFileExtension(extension);
    }

    private static Icon getIconForFileExtension(String extension){
        if (JAVA.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Java;
        else if (XML.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Xml;
        else if (JAR.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Archive;
        else if (HTM.equalsIgnoreCase(extension) | HTML.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Html;
        else if (XSD.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.XsdFile;
        else if (LOG.equalsIgnoreCase(extension) | TXT.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Text;
        else if (JSON.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Json;
        else if (JS.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.JavaScript;
        else if (JSP.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Jsp;
        else if (JSPX.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Jspx;
        else if (FORM.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.UiForm;
        else if (DTD.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Dtd;
        else if (CSS.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Css;
        else if (MF.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Manifest;
        else if (WSDL.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.WsdlFile;
        else if (CONFIG.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Config;
        else if (PROPERTIES.equalsIgnoreCase(extension))
            return AllIcons.FileTypes.Properties;

        return AllIcons.FileTypes.Unknown;
    }
}
